<template>
  <div>
    <el-card>
      <template #header> 管理员信息 </template>
      <span>管理员ID: {{ userInfo.id }}</span>
      <el-divider></el-divider>
      <span>账号: {{ userInfo.account }}</span>
      <el-divider></el-divider>
      <span>用户组: {{ userInfo.userGroup }}</span>
      <el-divider></el-divider>
      <span>创建时间: {{ userInfo.ctime | YMD }}</span>
      <el-divider></el-divider>
      <el-upload
        class="avatar-uploader"
        action="http://sell.h5.itsource.cn:8087/users/avatar_upload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="userInfo.imgUrl" :src="userInfo.imgUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-card>
  </div>
</template>

<script>
import { api_users_avataredit } from "@/api/user";
import { mapActions, mapState } from "vuex";
export default {
  computed: {
    ...mapState("user", ["userInfo"]),
  },
  data() {
    return {};
  },
  methods: {
    ...mapActions("user", ["A_userInfo"]),
    async handleAvatarSuccess(res) {
      let { code, imgUrl } = res;
      if (code === 0) {
        // 再次发送请求
        let res2 = await api_users_avataredit({ imgUrl });
        let { code } = res2.data;
        if (code === 0) {
          this.A_userInfo();
        }
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = /(png|jpeg)/.test(file.type);
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG PNG格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="scss" scoped>
.avatar-uploader :deep(.el-upload) {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>